package com.deanlib.cabinet.ui.view;

import android.text.TextUtils;

import com.deanlib.cabinet.R;
import com.deanlib.cabinet.base.BaseActivity;
import com.deanlib.cabinet.data.event.UserStateEvent;
import com.deanlib.cabinet.data.network.NetWorkManager;
import com.deanlib.cabinet.databinding.ActivityRegisterBinding;
import com.deanlib.cabinet.utils.UserManager;
import com.deanlib.plantern.utils.MD5Utils;
import com.deanlib.plantern.utils.PopupUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 注册
 * Created by Dean on 2022/6/14
 */
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> {
    @Override
    public void initView() {
        binding.btnRegister.setOnClickListener(view->{
            String username = binding.etUsername.getText().toString().trim();
            if (TextUtils.isEmpty(username)){
                PopupUtils.sendToast(R.string.username_cannot_be_empty);
                return;
            }
            String password = binding.etPassword.getText().toString();
            if (TextUtils.isEmpty(password)){
                PopupUtils.sendToast(R.string.password_cannot_be_empty);
                return;
            }
            String password2 = binding.etPassword2.getText().toString();
            if (!password.equals(password2)){
                PopupUtils.sendToast(R.string.two_passwords_are_inconsistent);
                return;
            }
            request(NetWorkManager.getRequest().doRegister(username, MD5Utils.md5(password)), data->{
                PopupUtils.sendToast(R.string.registered_successful);
                UserManager.getInstance().save(data.orElse(null));
                EventBus.getDefault().postSticky(new UserStateEvent());
                finish();
            });
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }
}
